package lena;

import java.awt.image.BufferedImage;
import lib.Actor;
import lib.ActorAddress;
import lib.Message;


public class BinarizeActor extends Actor{
	
	public BinarizeActor(ActorAddress superActor,ActorAddress myAddress) throws Exception {
		super(superActor, myAddress, "BinarizeActor");
		startActor();
	}
	
	protected boolean standardRoutine() throws Exception {
		this.standardRoutineIsUsed=false;
		return true;
	}
	
	protected boolean handleMessage(Message msg) throws Exception {
		if (msg.getOperation().equals("Terminate")) {
			return false;
		}
		
		System.out.println("BinarizeActor : Img received.");
		ImgChunk chunk = (ImgChunk) msg.getContent();
		BufferedImage temp = chunk.getImage();
		BufferedImage newimg = new BufferedImage(temp.getWidth(),temp.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
		int []rgb = new int[temp.getWidth() * temp.getHeight()];
		temp.getRGB(0,0,temp.getWidth(),temp.getHeight(),rgb,0,temp.getWidth());
		newimg.setRGB(0,0,temp.getWidth(),temp.getHeight(),rgb,0,temp.getWidth());
		ImgChunk ToBeSent = new ImgChunk(chunk.getPart(),chunk.getNbPartTotal(),newimg,chunk.getiDImage());
		sendMsg(new Message(this.myAddress, "BinarizeActor","RecomposerActor", "", ToBeSent));
		return true; 
	}
	
}
